package hoyo.zqy.system.controller;

import hoyo.zqy.basic.util.AjaxResult;
import hoyo.zqy.basic.util.PageList;
import hoyo.zqy.system.domain.SystemDictionaryType;
import hoyo.zqy.org.query.DepartmentQuery;
import hoyo.zqy.system.service.ISystemDictionaryTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 关于单位的接口
 */
@RestController
@RequestMapping("dicType")
@Api(tags = "数据字典维护接口")
//@CrossOrigin
public class SystemDictionaryTypeController {
    //注入service
    @Autowired
    private ISystemDictionaryTypeService systemDictionaryTypeService;


    // 查询全部的接口 DepartmentQuery departmentQuery
    @GetMapping
    public List<SystemDictionaryType> findDepartments() {
        return systemDictionaryTypeService.loadAll();
    }

    //    查询单个的接口
    @GetMapping("/{id}")
    public SystemDictionaryType findDepartment(@PathVariable("id") Long id) {
        System.out.println(systemDictionaryTypeService.loadById(id));
        return systemDictionaryTypeService.loadById(id);
    }

    /**
     * 新增和修改的接口
     * PutMapping  restful风格put类型请求
     *
     * @RequestBody 接收一个json格式的参数
     */

    @PutMapping
    @ApiOperation(value = "新增和修改的接口", notes = "如果有id就是修改，否则就是新增")
    public AjaxResult addDepartment(@RequestBody SystemDictionaryType systemDictionaryType) {
        try {
            if (systemDictionaryType.getId() == null) {
                System.out.println("新增");
                System.out.println(systemDictionaryType);
                systemDictionaryTypeService.save(systemDictionaryType);
            } else {
                System.out.println(systemDictionaryType);
                System.out.println("修改");
                systemDictionaryTypeService.update(systemDictionaryType);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            return new AjaxResult(false,"失败了,正在殴打程序猿中");
        }
    }

    /**
     * 删除的接口
     *
     * @PathVariable 动态解析url里面的id 赋值给 Long id
     */
    @DeleteMapping("/{id}")
    public AjaxResult deleDepartment(@PathVariable("id") Long id) {
        try {
            System.out.println(id + "删除");
            systemDictionaryTypeService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            return new AjaxResult(false,"失败了,正在殴打程序猿中");
        }
    }

    @PostMapping
    @ApiOperation(value = "分页查询" )
    public PageList<SystemDictionaryType> findDepartmentQuery(@RequestBody DepartmentQuery departmentQuery) {
        return systemDictionaryTypeService.queryPage(departmentQuery);
    }

    //    批量删除的接口
    @PatchMapping
    public AjaxResult deleBatch(@RequestBody Long[] ids) {
        try {
            System.out.println(Arrays.toString(ids));
            systemDictionaryTypeService.deleBatch(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            return new AjaxResult(false,"失败了,正在殴打程序猿中");
        }
    }





////    根据条件查询，拥有联级datail
//@PostMapping("/datail")
//public QueryResult<SystemDictionaryType> findDetail(@RequestBody id id) {
//    System.out.println(id);
//    return systemDictionaryTypeService.findDetail(departmentQuery);
//}

}
